package com.service.school.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.VClassInfo;
import com.pageModel.ClassInfo;
import com.pageModel.DataGrid;
import com.service.school.ClassInfoServiceI;

@Service(value = "classInfoService")
public class ClassInfoServiceImpl implements ClassInfoServiceI {

	private BaseDaoI<VClassInfo> classInfoDao;
	
	public BaseDaoI<VClassInfo> getClassInfoDao() {
		return classInfoDao;
	}
	@Autowired
	public void setClassInfoDao(BaseDaoI<VClassInfo> classInfoDao) {
		this.classInfoDao = classInfoDao;
	}
	
	@Override
	public DataGrid datagrid(ClassInfo classInfo) {
		DataGrid dg = new DataGrid();
		String hql="from VClassInfo v where v.isEnable='是' ";
		if(classInfo.getClassName()!=null&&!classInfo.getClassName().trim().equals("")){
			hql+=" and v.className='"+classInfo.getClassName()+"'";
		}
		if(classInfo.getGradeName()!=null&&!classInfo.getGradeName().trim().equals("")){
			hql+=" and v.gradeName='"+classInfo.getGradeName()+"'";
		}
		if(classInfo.getCollegeName()!=null&&!classInfo.getCollegeName().trim().equals("")){
			hql+=" and v.collegeName='"+classInfo.getCollegeName()+"'";
		}
		if(classInfo.getMajorName()!=null&&!classInfo.getMajorName().trim().equals("")){
			hql+=" and v.majorName='"+classInfo.getMajorName()+"'";
		}
		String totalHql = "select count(*) " + hql;
		List<VClassInfo> l = classInfoDao.find(hql, classInfo.getPage(), classInfo.getRows());
		List<ClassInfo> nl = new ArrayList<ClassInfo>();
		if (l != null && l.size() > 0) {
			for (VClassInfo t : l) {
				ClassInfo u = new ClassInfo();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(classInfoDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}

}
